import axios from 'axios'
import { Toast } from 'vant';
import { getCookie } from './unit/cookie'

axios.defaults.timeout = 500000; //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置请求头

axios.interceptors.request.use(
    function(config) {
        let token = getCookie("jwtToken")
        if (token) {
            config.headers.common['Authorization'] = token
        }
        return config
    },
    function(error) {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(response => {
    return response
}, error => {
    if (error.response.status === 406) {
        Toast(error.response.data.errMsg)
    }
    if (error.response.status === 500) {
        Toast("服务器错误")
    }
    return Promise.resolve(error.response)
})

export default axios